/***************************************************
 @Name :        小铭同学
 @Time :        2025-09-03
 @Email :       LiaoMingWJ233@163.com
 @Comment :     温湿度传感器 图表 控件类
 @FileName :    th_chart.h
 @Version :     1.0
****************************************************/


#ifndef TH_CHART_H
#define TH_CHART_H

#include <QWidget>
#include <QtCharts/QChartView>
#include <QtCharts/QLineSeries>
#include <QtCharts/QValueAxis>
#include <QTimer>
#include <QLabel>
#include <QHBoxLayout>
#include <QVBoxLayout>
#include <QResizeEvent>

QT_CHARTS_USE_NAMESPACE

class TH_chart : public QWidget
{
    Q_OBJECT

public:
    explicit TH_chart(QWidget *parent = nullptr);
    ~TH_chart();

    void setTemperature(float temp);
    void setHumidity(float humidity);
    void setRange(float minTemp, float maxTemp, float minHumidity, float maxHumidity);
    void setAnimationEnabled(bool enabled);
    
    // 添加尺寸设置方法
    void setChartSize(int width, int height);
    void setMinimumChartSize(int minWidth, int minHeight);
    void setFixedChartSize(int width, int height);

private slots:
    void updateAnimation();

private:
    void initUI();
    void initChart();
    void updateDisplay();
    
    // 添加资源清理方法
    void cleanup();

    // 使用指针并初始化为nullptr
    QChartView *chartView = nullptr;
    QChart *chart = nullptr;
    QLineSeries *tempSeries = nullptr;
    QLineSeries *humiditySeries = nullptr;
    QValueAxis *axisX = nullptr;
    QValueAxis *axisYTemp = nullptr;
    QValueAxis *axisYHumidity = nullptr;
    QLabel *tempLabel = nullptr;
    QLabel *humidityLabel = nullptr;
    QTimer *animationTimer = nullptr;

    QVector<QPointF> tempData;
    QVector<QPointF> humidityData;

    float currentTemp = 0.0f;
    float currentHumidity = 0.0f;
    float tempMin = 0.0f;
    float tempMax = 50.0f;
    float humidityMin = 0.0f;
    float humidityMax = 100.0f;
    float animationProgress = 0.0f;
    bool animationEnabled = true;

    static const int MAX_DATA_POINTS = 50;
};

#endif // TH_CHART_H
